﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SMTC.Model.Model;
using SMTC.BO.Report;
using System.Web.UI.DataVisualization.Charting;
using SMTC.BO.AnalyzePara;
using SMTC.WebCommon.Common;

public partial class ReportManagment_ReportChartView : BasePage
{
    private ReportService _reportServices = new ReportService();

    public SMT_paraModel Para
    {
        get
        {
            AnalyzeParaQueryServices service = new AnalyzeParaQueryServices();
            if (ViewState["Para"] != null)
            {
                return ViewState["Para"] as SMT_paraModel;
            }
            else
            {
                ViewState["Para"] = service.getPara();
                return ViewState["Para"] as SMT_paraModel;
            }
        }

    }

    public DateTime start
    {
        set
        {
            ViewState["start"] = value;
        }

        get
        {
            try
            {
                return DateTime.Parse(ViewState["start"].ToString());
            }
            catch
            {
                return new DateTime(1999, 1, 1);
            }
        }
    }

    public DateTime end
    {
        set
        {
            ViewState["end"] = value;
        }

        get
        {
            try
            {
                return DateTime.Parse(ViewState["end"].ToString());
            }
            catch
            {
                return new DateTime(2999, 12, 31);
            }
        }
    }

    public string PlanVersion
    {
        set
        {
            ViewState["PlanVersion"] = value;
        }

        get
        {
            try
            {
                return ViewState["PlanVersion"].ToString();
            }
            catch
            {
                return "";
            }
        }
    }

    public string PUVersion
    {
        set
        {
            ViewState["PUVersion"] = value;
        }

        get
        {
            try
            {
                return ViewState["PUVersion"].ToString();
            }
            catch
            {
                return "";
            }
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request["start"] != null)
        {
            try
            {
                this.start = DateTime.Parse(Request["start"].ToString());
            }
            catch
            {
                this.start = new DateTime(1999, 1, 1);
            }
        }

        if (Request["end"] != null)
        {
            try
            {
                this.end = DateTime.Parse(Request["end"].ToString());
            }
            catch
            {
                this.end = new DateTime(2999, 12, 31);
            }
        }

        if (Request["version"] != null)
        {
            this.PlanVersion = Request["version"].ToString();
        }

        if (Request["PUVersion"] != null)
        {
            this.PUVersion = Request["PUVersion"].ToString();
        }

        if (Request["buName"] != null)
        {
            this.ChartDataBind(Request["buName"].ToString());
        }

    }

    protected void ChartDataBind(string buName)
    {
        List<ReportDataSourceModel> plan = _reportServices.GetReportPlan(buName, PlanVersion);
        List<ReportDataSourceModel> capacity = _reportServices.GetReportCapacity(buName, start, end, PUVersion);
        List<ReportDataSourceModel> teep = new List<ReportDataSourceModel>();

        if (plan.Count == 12 && capacity.Count == 12)
        {
            for (int index = 0; index < 12; index++)
            {
                try
                {
                    ReportDataSourceModel item = new ReportDataSourceModel();
                    item.BU = plan[index].BU;
                    item.Discrib = plan[index].Discrib;
                    item.Year = plan[index].Year;
                    if (capacity[index].Capacity == 0) throw new Exception();
                    item.Capacity = plan[index].Capacity / capacity[index].Capacity;
                    teep.Add(item);
                }
                catch
                {
                    ReportDataSourceModel item = new ReportDataSourceModel();
                    item.BU = "";
                    item.Discrib = "";
                    item.Year = plan[index].Year;
                    item.Capacity = 0;
                    teep.Add(item);
                    continue;
                }
            }
        }

        foreach (var item in plan)
        {
            item.Capacity = Math.Round(item.Capacity / 10000, 1);
        }

        foreach (var item in capacity)
        {
            item.Capacity = Math.Round(item.Capacity / 10000, 1);
        }
        //能力
        //Chart1.Series["Series2"].Points.DataBind(capacity, "Year", "Capacity", "");
        //Chart1.Series["Series2"].IsValueShownAsLabel = true;

        //需求
        Chart1.Series["Series1"].Points.DataBind(teep, "Year", "Capacity", "");
        //Chart1.Series["Series1"].IsValueShownAsLabel = true;
        Chart1.Series["Series1"]["PointWidth"] = "0.5";
        Chart1.Series["Series1"]["DrawingStyle"] = "Cylinder";
        Chart1.ChartAreas["ChartArea1"].AxisX.Interval = 1;
        Chart1.ChartAreas["ChartArea1"].BackColor = System.Drawing.Color.Transparent;
        Chart1.ChartAreas["ChartArea1"].AxisX.MajorGrid.LineColor = System.Drawing.Color.Blue;
        Chart1.ChartAreas["ChartArea1"].AxisY.MajorGrid.LineColor = System.Drawing.Color.Blue;
        Chart1.ChartAreas["ChartArea1"].AxisX.LineColor = System.Drawing.Color.Blue;
        Chart1.ChartAreas["ChartArea1"].AxisY.LineColor = System.Drawing.Color.Blue;
        Chart1.ChartAreas["ChartArea1"].AxisY.LineWidth = 2;
        Chart1.ChartAreas["ChartArea1"].AxisX.LineWidth = 2;

        Chart1.ChartAreas["ChartArea2"].AxisX.Interval = 1;
        Chart1.ChartAreas["ChartArea2"].BackColor = System.Drawing.Color.Transparent;
        Chart1.ChartAreas["ChartArea2"].AxisX.MajorGrid.LineColor = System.Drawing.Color.Blue;
        Chart1.ChartAreas["ChartArea2"].AxisY.MajorGrid.LineColor = System.Drawing.Color.Blue;
        Chart1.ChartAreas["ChartArea2"].AxisX.LineColor = System.Drawing.Color.Blue;
        Chart1.ChartAreas["ChartArea2"].AxisY.LineColor = System.Drawing.Color.Blue;
        Chart1.ChartAreas["ChartArea2"].AxisY.LineWidth = 2;
        Chart1.ChartAreas["ChartArea2"].AxisX.LineWidth = 2;

        Chart1.ChartAreas["ChartArea3"].AxisX.Interval = 1;
        Chart1.ChartAreas["ChartArea3"].BackColor = System.Drawing.Color.Transparent;
        Chart1.ChartAreas["ChartArea3"].AxisX.MajorGrid.LineColor = System.Drawing.Color.Blue;
        Chart1.ChartAreas["ChartArea3"].AxisY.MajorGrid.LineColor = System.Drawing.Color.Blue;
        Chart1.ChartAreas["ChartArea3"].AxisX.LineColor = System.Drawing.Color.Blue;
        Chart1.ChartAreas["ChartArea3"].AxisY.LineColor = System.Drawing.Color.Blue;
        Chart1.ChartAreas["ChartArea3"].AxisY.LineWidth = 2;
        Chart1.ChartAreas["ChartArea3"].AxisX.LineWidth = 2;
        //Chart1.ChartAreas["ChartArea1"].Area3DStyle.Enable3D = true;

        Chart1.Series["Series3"].Points.DataBind(plan, "Year", "Capacity", "");
        Chart1.Series["Series3"].BorderWidth = 2;
        Chart1.Series["Series3"].Color = System.Drawing.Color.Green;
        Chart1.Series["Series3"]["PointWidth"] = "0.5";

        //Chart1.Series["Series3"].IsValueShownAsLabel = true;

        Chart1.Series["Series4"].Points.DataBind(capacity, "Year", "Capacity", "");
        Chart1.Series["Series4"].BorderWidth = 2;
        Chart1.Series["Series4"].Color = System.Drawing.Color.Green;
        Chart1.Series["Series4"]["PointWidth"] = "0.5";
        //Chart1.Series["Series4"].IsValueShownAsLabel = true;
        lbl_bu.Text = buName;
    }
    protected void Chart1_DataBound(object sender, EventArgs e)
    {
        Chart mychart = sender as Chart;
        foreach (var item in mychart.Series[0].Points)
        {
            if (item.YValues[0]*100 > (double)Para.RedAlert)
            {
                item.Color = System.Drawing.Color.Red;
            }
            else if (item.YValues[0]*100 >= (double)Para.YellowAlert)
            {
                item.Color = System.Drawing.Color.Yellow;
            }
            else
            {
                item.Color = System.Drawing.Color.Green;
            }
        }
    }
}